<template>
<view class="container">
  <form bindsubmit="editForm">
    <view class="bigbox">
      <view class="jtxx clearfix">
        <view class="jtxx_l left">姓名</view>
        <view class="jtxx_r right">
          <input class="xb" type="text" :value="userInfo.truename" name="truename"></input>
        </view>
      </view>
      <view class="jtxx clearfix">
        <view class="jtxx_l left">生日</view>
        <view class="jtxx_r right">
          <picker mode="date" name="birthday" :value="userInfo.birthday" bindchange="birthChange">
            <view class="birthPicker">{{userInfo.birthday}}</view>
          </picker>
        </view>
      </view>
    </view>
    <button form-type="submit" class="btn">保存</button>
  </form>
</view>
</template>

<script>
import common from '../../utils/common.js'
var app = getApp();

export default {
  data() {
    return {
      mer_id: 0,
      userInfo: {}
    };
  },

  onShareAppMessage: function () {},
  onReachBottom: function () {},
  onPullDownRefresh: function () {},
  onUnload: function () {},
  onHide: function () {},
  onShow: function () {},
  onReady: function () {},
  onLoad: function (options) {
    if (!options.mer_id) {
      options.mer_id = app.merchantOwn.mer_id;
    }

    this.setData({
      mer_id: options.mer_id
    });

    if (!app.globalData.userInfo) {
      var that = this; //调用应用实例的方法获取全局数据

      common.getUserInfo(function (userInfo) {
        if (!userInfo.phone) {
          wx.navigateTo({
            url: "../my/newnumber"
          });
        } else {
          that.setMainContent();
        }
      }, 'setting');
    } else {
      this.setMainContent();
    }
  },
  components: {},
  props: {},
  methods: {
    setMainContent: function () {
      this.setData({
        userInfo: app.globalData.userInfo
      });
    },
    birthChange: function (e) {
      var userInfo = this.userInfo;
      userInfo.birthday = e.detail.value;
      this.setData({
        userInfo: userInfo
      });
    },
    editForm: function (e) {
      common.post('My&a=personal', e.detail.value, "saveFormResult", this);
    },
    saveFormResult: function (result) {
      app.globalData.userInfo.truename = result.truename;
      app.globalData.userInfo.birthday = result.birthday;
      wx.hideToast();
      wx.showModal({
        title: '提示',
        content: "修改成功",
        showCancel: false
      });
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
.bigbox {
  width: 100%;
  overflow: hidden;
  margin-top: 40rpx;
  border-top: 2rpx solid #f1f1f1;
  border-bottom: 2rpx solid #f1f1f1;
  background:white;
}
.jtxx {
  width: 94%;
  height: 104rpx;
  line-height: 104rpx;
  margin: 0 2%;
  padding: 0 1%;
  border-bottom: 1rpx solid #f1f1f1;
}
.jtxx_l {
  width: 20%;
}
.jtxx_r {
  width: 78%;
}
.jtxx_r input{
  height:104rpx;
  line-height:104rpx;
}
.jtxx_r .birthPicker{
  background: url(https://hf.pigcms.com/static/wxapp/rili.png) no-repeat right center;
  background-size: 44rpx 44rpx;
}


.btn {
    width: 80%;
    height: 80rpx;
    line-height:80rpx;
    margin: 30rpx 12% 0;
    background: #d00000;
    color: #FFF;
    font-size:32rpx;
}
</style>